<?php
namespace app\admin\controller;
use think\Controller;

Class CategoryWords extends BaseAdmin
{

	//关联词列表
    public function lst()
    {
    	
        
        $CategoryWordsRes=db('tp_category_words')->alias('a')->field('a.*,b.cate_name')->join('tp_category b','a.category_id=b.id')->paginate(5);
        $this->assign('CategoryWordsRes',$CategoryWordsRes);
        return $this->fetch('list');
    }

    //关联词添加
    public function add()
    {
        //顶级栏目信息
        $TopCates=db('tp_category')->where(['pid'=>0])->select();
        $this->assign('TopCates',$TopCates);
    	 
        if(request()->isPost()){
        	$data=input('post.');
            // dump($data);die;
        	if($data['link_url']&&stripos($data['link_url'],'http://')===false){
        		$data['link_url']='http://'.$data['link_url'];
        	}

        
        	//验证
        	// $validate = new \app\admin\validate\CategoryWords;
        	// if (!$validate->check($data)) {
        	// 	$this->error($validate->getError());
        	// }
        	
        	$add=db('tp_category_words')->insert($data);
        	if($add){
        		$this->success('保存成功','lst');
        	}
        	else{
        		$this->error('保存失败');
        	}

        }
        return $this->fetch();

    }

    //关联词编辑
    public function edit($id)
    {
    	$CategoryWordsRes=db('tp_category_words')->where(array('id'=>$id))->find();
    	$this->assign('CategoryWordsRes',$CategoryWordsRes);

        //顶级栏目信息
        $TopCates=db('tp_category')->where(['pid'=>0])->select();
        $this->assign('TopCates',$TopCates);
    	if(request()->isPost()){
        	$data=input('post.');
        	if($data['link_url']&&stripos($data['link_url'],'http://')===false){
        		$data['link_url']='http://'.$data['link_url'];
        	}

        	$up=db('tp_category_words')->where(array('id'=>$id))->update($data);

        	if($up!==false){
        		$this->success('保存成功','lst');
        	}
        	else{
        		$this->error('保存失败');
        	}

        }
        return $this->fetch();
    }

    //关联词删除
    public function del($id)
    {
    	$del=db('tp_category_words')->delete($id);
    	if($del){
    		$this->success('删除成功','lst');

    	}else{
            $this->error('删除失败');
    	}
    	
        return $this->fetch();
    }


}